//
//  WBHomeCell.swift
//  Weibo
//
//  Created by xu on 2017/3/24.
//  Copyright © 2017年 xuantong. All rights reserved.
//

import UIKit
import SnapKit

class WBHomeCell: UITableViewCell {

    var bottomViewTopConstraint: Constraint?
    
    // 提供一个数据(statusViewModel) 供外界赋值
    var statusViewModel:WBStatusViewModel? {
        
        didSet {
            // 给原创微博 转发微博 底部视图 进行赋值
              originalView.statusViewModel = statusViewModel
            
            //判断是否显示转发微博, == nil代表没有转发微博
            //如果没有转发cells上面显示两个控件(原创 和 底部视图)
            
            
            ///卸载约束
            bottomViewTopConstraint?.deactivate()

            if let _  = statusViewModel?.homeModel?.retweeted_status {
                //不是空
                //赋值
                retweetView.statusViewModel = statusViewModel
                
                //修改按钮底部的约束
                buttonView.snp.makeConstraints({ (make) in
                    
                    bottomViewTopConstraint = make.top.equalTo(retweetView.snp.bottom).constraint

                    
                })
                
                //显示
                retweetView.isHidden = false
                
            }else{
        
                buttonView.snp.makeConstraints({ (make) in
                    bottomViewTopConstraint = make.top.equalTo(originalView.snp.bottom).constraint
                })

                 // 隐藏
                retweetView.isHidden = true
                
            }
        }
    }
    
    
    //重写init方法
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //MARK: 设置视图
    private func setupUI(){
    
        //添加控制
        contentView.addSubview(originalView)
        contentView.addSubview(retweetView)
        contentView.addSubview(buttonView)
        
        //添加约束
        originalView.snp.makeConstraints { (make) in
            make.top.equalTo(contentView).offset(8)
            make.left.right.equalTo(contentView)
            /// 原创微博的底部约束在原创微博内部已经计了
            
        }
    

        
        retweetView.snp.makeConstraints { (make) in
            make.left.right.equalTo(contentView)
            make.top.equalTo(originalView.snp.bottom)
        }

        
        
        buttonView.snp.makeConstraints { (make) in
            
            bottomViewTopConstraint = make.top.equalTo(retweetView.snp.bottom).constraint
            make.left.right.bottom.equalTo(contentView)
//            make.height.equalTo(35)
        }
        
        
    }
    
    
    
    
    ////MARK: 懒加载实例化控制
    // 原创微博
    private lazy var originalView:WBStatusOriginalView = WBStatusOriginalView()
    //转发微博
    private lazy var retweetView :WBStatusRetweetView = WBStatusRetweetView()
    //按钮
    private lazy var buttonView :WBStatusBottomView = WBStatusBottomView()
       
}
